package com.campusflea.person;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.campusflea.DateBase.ShopInfo;
import com.campusflea.GoodsDetailsActivity;
import com.campusflea.R;
import com.campusflea.adapter.MyAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

/**
 * Created by Administrator on 2017/6/14.
 */

public class Person_sale extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
    private RecyclerView mRecyclerview;
    private DemoAdapter mAdapter;
    List<DataModel> mList;
    List<ShopInfo>  list;
    private Button btn_hui;
    private HashMap<String, Object> map;
    private static final int REFRESH_COMPLETE = 0X110;
    private SwipeRefreshLayout swipeRefreshLayout;
    int colors[]={R.color.blue,
            R.color.red,
            R.color.darkorange

    };
    private Handler mHandler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case REFRESH_COMPLETE:
//                    mDatas.addAll(Arrays.asList("Lucene", "Canvas", "Bitmap"));
                    mAdapter.notifyDataSetChanged();
                    swipeRefreshLayout.setRefreshing(false);
                    break;
            }
        };
    };
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sale);
        btn_hui= (Button) findViewById(R.id.button7);
        btn_hui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRecyclerview= (RecyclerView) findViewById(R.id.recyclerview);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL,false));
        mAdapter=new DemoAdapter(this);
        mRecyclerview.setAdapter(mAdapter);
        swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
        swipeRefreshLayout.setOnRefreshListener(Person_sale.this);
        initData1();
        mAdapter.setOnItemClickListener(new DemoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(Person_sale.this,position+"click",Toast.LENGTH_SHORT).show();
                ShopInfo shopInfo = new ShopInfo();
                shopInfo=list.get(position);
                Intent intent =new Intent(Person_sale.this, GoodsDetailsActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("sale",shopInfo);
                intent.putExtra("compare",1);
                intent.putExtras(mBundle);
                startActivity(intent);

            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(Person_sale.this,position+"longclick",Toast.LENGTH_SHORT).show();
                DataModel da=new DataModel();
                da.name="woshi你大爷";
                da.url="http://offlintab.firefoxchina.cn/static/img/search/baidu_web.png";
                //getDynamicPicture().getUrl();
                da.type=2;
                da.content="4a4s5d4a4wsd";
                da.price="price"+1;
                add(da);
            }
        });

    }
    public void add(Object obj){
        mList.add(0, (DataModel) obj);
        mAdapter.notifyItemInserted(0);
        mAdapter.notifyItemRangeChanged(0,mList.size()-2);

    }

//    private void initData() {
//
//
//        List<DataModel> mList=new ArrayList<>();
//        for (int i=0; i<20;i++){
//                int type=(int)((Math.random()*3)+1);
//                DataModel data=new DataModel();
//                data.avatarColor=colors[type-1];
//                data.mimage=R.mipmap.logo;
//                data.type=2;
//                data.name="name"+i;
//                data.content="content"+i;
//                data.price="price"+i;
//                data.contentColor=colors[(type+1)%3];
//                mList.add(data);
//        }
//        mAdapter.addList(mList);
//        mAdapter.notifyDataSetChanged();
//    }
    private  void initData1()
    {

        final String bql ="select * from ShopInfo";
         mList=new ArrayList<>();
        final BmobQuery<ShopInfo> query=new BmobQuery<ShopInfo>();
        query.setSQL(bql);
        query.doSQLQuery(new SQLQueryListener<ShopInfo>(){
            @Override
            public void done(BmobQueryResult<ShopInfo> bmobQueryResult, BmobException e) {
                 list = (List<ShopInfo>) bmobQueryResult.getResults();
                if(list.size()==0){
                    Toast.makeText(Person_sale.this,"获取信息失败",Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(Person_sale.this,"信息加载中。。。。",Toast.LENGTH_LONG).show();
                    for(int i=0;i<list.size();i++){
                        int type=(int)((Math.random()*3)+1);
                        DataModel data=new DataModel();
                        data.avatarColor=colors[type-1];
                        data.url=list.get(i).getBmobFile().getUrl();
                                //getDynamicPicture().getUrl();
                        data.type=2;
                        data.name=list.get(i).getShop_name();
                        data.content=list.get(i).getShop_disc();
                        data.price=list.get(i).getShop_price();
                        data.contentColor=colors[(type+1)%3];
                        mList.add(data);
                    }
                    mAdapter.addList(mList);
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
    }
}
